home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
8206
/
8206.xpi
/
content
/
prefs.js
< prev
next >
Wrap
Text File
|
2010-02-02
|
6KB
|
281 lines
var WiseStampPrefs = function()
{
var pub = {};
pub.PREFS = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("extensions.wisestamp.");
pub.getPrefType = function(pref_id)
{
var type = "";
try
{
type = this.PREFS.getPrefType(pref_id);
} catch (e)
{
return "";
}
return type;
}
pub.getPrefTypeStr = function(pref_id)
{
var type = this.getPrefType(pref_id);
switch (type)
{
case this.PREFS.PREF_STRING:
return "string";
case this.PREFS.PREF_INT:
return "int";
case this.PREFS.PREF_BOOL:
return "boolean";
}
return "";
}
pub.getPrefValue = function(pref_id)
{
var type = this.getPrefType(pref_id);
switch (type)
{
case this.PREFS.PREF_STRING:
return this.PREFS.getCharPref(pref_id);
case this.PREFS.PREF_INT:
return this.PREFS.getIntPref(pref_id);
case this.PREFS.PREF_BOOL:
return this.PREFS.getBoolPref(pref_id);
}
return "";
}
pub.setPrefValue = function(pref_id,value,type)
{
if (type == null)
type = this.getPrefType(pref_id);
switch (type)
{
case "string":
case this.PREFS.PREF_STRING:
this.PREFS.setCharPref(pref_id,value);
break;
case "int":
case this.PREFS.PREF_INT:
this.PREFS.setIntPref(pref_id,value);
break;
case "boolean":
case this.PREFS.PREF_BOOL:
this.PREFS.setBoolPref(pref_id,value == "true" ? true : false);
break;
}
}
pub.clearBranch = function(prefix)
{
/* remove preferences */
var list = this.PREFS.getChildList(prefix,{},{});
WiseStampUtils.log("prefs.js :: clearBranch :: clearing " + list.length + " elements under " + prefix);
for (var i = 0; i < list.length; i++)
{
try
{
this.PREFS.clearUserPref(list[i]);
} catch(e){}
}
}
pub.isModified = function(id)
{
return this.PREFS.prefHasUserValue(id);
}
///////////////////////////////////////////////////////////////////////////
// PREFERENCES FUNCTIONS
// Note: id2 is usefull for backword compatibility. If id is not present id2 will be fetched
pub.getBoolPref = function(id,default_val,id2)
{
var value = null;
try {
value = pub.PREFS.getBoolPref(id);
} catch (e)
{
if (id2 != undefined)
return this.getBoolPref(id2,default_val);
if (default_val == undefined)
value = false;
else
value = default_val;
}
return value;
}
pub.getIntPref = function(id,default_val,id2)
{
var value = null;
try {
value = pub.PREFS.getIntPref(id);
} catch (e)
{
if (id2 != undefined)
return this.getIntPref(id2,default_val);
if (default_val == undefined)
value = false;
else
value = default_val;
}
return value;
}
pub.getCharPref = function(id,default_val,id2)
{
var value = null;
try {
value = pub.PREFS.getCharPref(id);
} catch (e)
{
if (id2 != undefined)
return this.getCharPref(id2,default_val);
if (default_val == undefined)
value = "";
else
value = default_val;
}
return value;
}
pub.getComplexPref = function(id,default_val,id2)
{
var value = null;
try {
value = pub.PREFS.getComplexValue(id, Components.interfaces.nsISupportsString).data;
} catch (e)
{
if (id2 != undefined)
return this.getComplexPref(id2,default_val);
if (default_val == undefined)
value = "";
else
value = default_val;
}
return value;
}
pub.setComplexPref = function(id,value)
{
var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
str.data = value;
this.PREFS.setComplexValue(id, Components.interfaces.nsISupportsString, str);
}
///////////////////////////////////////////////////////////////////////////
pub.export = function(file)
{
// file is nsIFile, data is a string
var foStream = Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);
// use 0x02 | 0x10 to open file for appending.
foStream.init(file, 0x02 | 0x08 | 0x20, 0666, 0);
// if you are sure there will never ever be any non-ascii text in data you can
// also call foStream.writeData directly
var converter = Components.classes["@mozilla.org/intl/converter-output-stream;1"].createInstance(Components.interfaces.nsIConverterOutputStream);
converter.init(foStream, "UTF-8", 0, 0);
var list = this.PREFS.getChildList("",{},{});
for (var i = 0; i < list.length; i++)
{
try
{
var name = list[i];
var value = this.getPrefValue(name);
var type = this.getPrefTypeStr(name);
converter.writeString(name + '\r\n');
converter.writeString(type + '\r\n');
converter.writeString(value + '\r\n');
converter.writeString('---\r\n');
} catch(e)
{
WiseStampUtils.log("prefs.js :: export :: exception caught = " + e);
}
}
converter.close(); // this closes foStream
}
pub.import = function(file)
{
var fis = Components.classes["@mozilla.org/network/file-input-stream;1"].createInstance(Components.interfaces.nsIFileInputStream);
fis.init(file, -1, -1, 0);
var charset = "UTF-8";
var is = Components.classes["@mozilla.org/intl/converter-input-stream;1"].createInstance(Components.interfaces.nsIConverterInputStream);
// This assumes that fis is the nsIInputStream you want to read from
is.init(fis, charset, 1024, 0xFFFD);
var lis = is.QueryInterface(Components.interfaces.nsIUnicharLineInputStream);
if (lis instanceof Components.interfaces.nsIUnicharLineInputStream)
{
var name = {}, type = {}, line = {};
do
{
cont = lis.readLine(name);
cont = lis.readLine(type);
var value = "";
while (1)
{
cont = lis.readLine(line);
if (cont == false || line.value == '---')
break;
value += line.value;
}
WiseStampUtils.log("prefs.js :: import :: name = "+name.value+", type = "+type.value+", value = "+value);
this.setPrefValue(name.value,value,type.value);
} while (cont);
} else
alert("sorry!");
}
return pub;
} ();